package com.easylinkin.linkappapi.machinery.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.machinery.entity.TowerCraneCollisionAlarmRecord;
import com.easylinkin.linkappapi.machinery.mapper.TowerCraneCollisionAlarmRecordMapper;
import com.easylinkin.linkappapi.machinery.service.TowerCraneCollisionAlarmRecordService;
import com.easylinkin.linkappapi.machinery.vo.CollisionAlarmRecordVO;
import com.easylinkin.linkappapi.machinery.vo.CollisionAlarmStatisticsVO;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 塔吊防碰撞记录 服务实现类
 * </p>
 *
 * @author Xiaoyang
 * @since 2022-11-27
 */
@Service
public class TowerCraneCollisionAlarmRecordServiceImpl extends
    ServiceImpl<TowerCraneCollisionAlarmRecordMapper, TowerCraneCollisionAlarmRecord> implements
    TowerCraneCollisionAlarmRecordService {

  @Resource
  private LinkappUserContextProducer linkappUserContextProducer;

  @Override
  public List<CollisionAlarmStatisticsVO> getCollisionAlarmStatisticsByDriveNameTop(
      CollisionAlarmStatisticsVO statisticsVO) {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    statisticsVO.setTenantId(tenantId);
    List<CollisionAlarmStatisticsVO> list = baseMapper
        .getCollisionAlarmStatisticsByDriveNameTop(statisticsVO);
    return list;
  }

  @Override
  public List<CollisionAlarmRecordVO> getList(CollisionAlarmStatisticsVO statisticsVO) {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    statisticsVO.setTenantId(tenantId);
    List<TowerCraneCollisionAlarmRecord> list = baseMapper.getList(statisticsVO);
    List<CollisionAlarmRecordVO> voList = new ArrayList<>();
    if (CollectionUtil.isNotEmpty(list)) {
      list.forEach(p -> {
        CollisionAlarmRecordVO alarmRecordVO = new CollisionAlarmRecordVO();
        BeanUtils.copyProperties(p, alarmRecordVO);
        TowerCraneCollisionAlarmRecord oneByRefRecordId = baseMapper
            .getOneByRefRecordId(p.getId());
        if (null != oneByRefRecordId) {
          alarmRecordVO.setRefRecord(oneByRefRecordId);
          voList.add(alarmRecordVO);
        }
      });
    }
    return voList;
  }
}
